home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / BlobMgr / Demo Folder / Anagram.c next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  3.8 KB  |  183 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Blob Manager Demonstration:  Anagram module
  3.  *
  4.  * 19 July 1986    Paul DuBois
  5.  *
  6.  * 24 Dec 93
  7.  * - Added checking to see whether current state of answer is correct.  Dims
  8.  * Answer button if so.  This gives some feedback that the response is correct,
  9.  * which has been lacking.  Otherwise user has to recognize the word.
  10.  */
  11.  
  12. # include    "TransSkel.h"
  13.  
  14.  
  15. # include    "BlobMgr.h"
  16. # include    "BlobDemo.h"
  17.  
  18.  
  19. # define    vWord        10
  20. # define    letterSize    18
  21. # define    letterGap    2
  22.  
  23.  
  24. static WindowPtr        wind;
  25. static BlobSetHandle    receptors = (BlobSetHandle) nil;
  26. static short            hMid;
  27. static ControlHandle    next;
  28. static ControlHandle    answer;
  29.  
  30.  
  31. /*
  32.  * Check current state of answer.  If all receptors have a glob that's
  33.  * the correct letter, return true.  Can't just check whether BGlob (b) = b,
  34.  * since different blobs may have same letter, and it doesn't matter which
  35.  * of those is glued.
  36.  */
  37.  
  38. static Boolean
  39. CheckAnswer (void)
  40. {
  41. BlobHandle    b, g;
  42.  
  43.     for (b = FirstBlob (receptors); b != (BlobHandle) nil; b = NextBlob (b))
  44.     {
  45.         g = BGlob (b);
  46.         if (GetBRefCon (b) != GetBRefCon (g))
  47.             return (false);    /* wrong letter glued to blob */
  48.     }
  49.     return (true);
  50. }
  51.  
  52.  
  53. /*
  54.  * Generate the next anagram.  Get a word from the word picker,
  55.  * put each letter in a receptor and glue the receptor to itself.
  56.  * Then scramble the receptors and show them.
  57.  */
  58.  
  59. static void
  60. NextAnagram (void)
  61. {
  62. BlobHandle    b;
  63. short        i, h;
  64. char        *s;
  65.  
  66.     if (receptors != nil)        /* dispose of any previous sets */
  67.     {
  68.         HideBlobSet (receptors);
  69.         DisposeBlobSet (receptors);
  70.     }
  71.     receptors = NewBlobSet ();
  72.     s = (char *) PickWord ();
  73.     h = hMid - (s[0] * (letterSize + letterGap) - letterGap) / 2;
  74.     for (i = 1; i <= s[0]; ++i)
  75.     {
  76.         b = MakeCharBlob (receptors, false, infiniteGlue,
  77.                             false, h, vWord, s[i]);
  78.         h += letterSize + letterGap;
  79.         GlueGlob (b, b);
  80.     }
  81.     for (;;)                /* shuffle so that answer isn't correct right away */
  82.     {
  83.         ShuffleGlobSet (receptors);
  84.         if (!CheckAnswer ())
  85.             break;
  86.     }
  87.     ShowBlobSet (receptors);
  88.     HiliteControl (answer, normalHilite);
  89.     InvalRect (&wind->portRect);
  90. }
  91.  
  92.  
  93. /*
  94.  * Show answer to anagram.  This may not be unique.  For instance,
  95.  * "begin" can also be "being".  Clearing the globs shows the original
  96.  * attachments, as well as disabling any further transactions until
  97.  * the Next button is hit, since there won't be any globs to swap.
  98.  */
  99.  
  100. static void
  101. ShowAnswer (void)
  102. {
  103.     UnglueGlobSet (receptors);
  104. }
  105.  
  106.  
  107. static pascal void
  108. Mouse (Point pt, long t, short mods)
  109. {
  110. BlobHandle    b, d;
  111. ControlHandle    ctl;
  112.  
  113.     if (FindControl    (pt, wind, &ctl))
  114.     {
  115.         if (TrackControl (ctl, pt, nil))    /* button hit? */
  116.         {
  117.             if (ctl == next)
  118.                 NextAnagram ();
  119.             else if (ctl == answer)
  120.             {
  121.                 ShowAnswer ();
  122.                 HiliteControl (answer, dimHilite);
  123.             }
  124.         }
  125.     }
  126.     else if ((**answer).contrlHilite == normalHilite)    /* answer not correct yet */
  127.     {
  128.         BlobClick (pt, t, nil, receptors);
  129.         if (CheckAnswer ())
  130.             HiliteControl (answer, dimHilite);
  131.     }
  132. }
  133.  
  134.  
  135. static pascal void
  136. Update (Boolean resized)
  137. {
  138.     DrawControls (wind);
  139.     DrawBlobSet (receptors);
  140. }
  141.  
  142.  
  143. static pascal void
  144. Activate (Boolean active)
  145. {
  146.     if (active)
  147.     {
  148.         SetDragRects (wind);
  149.         SetBCPermissions (false, false, false, true, true);
  150.     }
  151. }
  152.  
  153.  
  154. void
  155. AnagramInit (void)
  156. {
  157. Rect    r;
  158.  
  159.     SkelWindow (wind = GetDemoWind (anaWindRes),
  160.                 Mouse,            /* mouse clicks */
  161.                 nil,            /* key clicks */
  162.                 Update,            /* updates */
  163.                 Activate,        /* activate/deactivate events */
  164.                 nil,            /* close window */
  165.                 DoWClobber,        /* dispose of window */
  166.                 nil,            /* idle proc */
  167.                 false);            /* irrelevant, since no idle proc */
  168.  
  169.     hMid = wind->portRect.right / 2;
  170.     SetRect (&r, 0, 0, 80, 20);
  171.     OffsetRect (&r, hMid - 40 - 90 / 2, wind->portRect.bottom - 25);
  172.     next = NewControl (wind, &r, "\pNext", true, 0, 0, 0,
  173.                             pushButProc, 0L);
  174.     OffsetRect (&r, 90, 0);
  175.     answer = NewControl (wind, &r, "\pAnswer", true, 0, 0, 0,
  176.                             pushButProc, 0L);
  177.  
  178.     SetCharBlobSize (letterSize);
  179.     NextAnagram ();
  180.  
  181.     MakeFrontWind (wind);
  182. }
  183.